package com.example.springbootexamples.service.impl;

import com.example.springbootexamples.domain.T1;
import com.example.springbootexamples.domain.T2;
import com.example.springbootexamples.mapper.T1Mapper;
import com.example.springbootexamples.mapper.T2Mapper;
import com.example.springbootexamples.service.T1Service;
import com.example.springbootexamples.service.T2Service;
import com.example.springbootexamples.util.SleepUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@Service
public class T2ServiceImpl implements T2Service {

    private Logger log= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private T2Mapper t2Mapper;
    @Autowired
    private T1Service t1Service;


    @Override
    public List<T2> save(List<T1> list) {
       /* synchronized (this) {
            List<T1> t1List = t1Mapper.queryAll();
            Map<String, Object> map = new HashMap<>();
            for (T1 t1 : t1List) {
                map.put(t1.getName(), t1);
            }

            for (T1 t1 : list) {
                if (map.get(t1.getName()) == null) {
                    log.info("insert {}", t1.getName());
                    t1Mapper.insert(t1);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    log.info("exist {}", t1.getName());
                }
            }
        }*/
        List<T1> t1List=new ArrayList<>();
        for(int i=0;i<10;i++) {
            T1 t = new T1();
            t.setName("bob"+i);
            t.setPass("p"+i);
            t.setDate(new Date());
            //T1 t1 = t1Repository.save(t);
            t1List.add(t);
        }
        t1Service.save(t1List);
        SleepUtil.sleep(1000);
        return null;
    }
}
